layui.use(["jquery","table","element","layer","laydate"],function () {
    var $ = layui.jquery;
    var table = layui.table;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var data;
    $.ajaxSettings.async = false;
    $.ajax({
        type: "get",
        url: "/Manage/getCash",
        success: function (obj) {
            data = obj;
        }
    });
    


    let newData = JSON.parse(JSON.stringify(data));

    
    var indexTable = table.render({
        elem: "#indexTable",
        toolbar:"#insert",
        autoSort: false,
        page: true,
        cols: [[
            {field: "cashFlowId", title: "现金流Id", },
            {field: "charge", title: "收费标准",},
            {field: "startTime", title: "开始时间"},         
            {field: "action", title: "操作", toolbar: "#actions", width: 200},
        ]],
        data: newData
    });
    
    //头工具栏
    table.on("toolbar(indexTable)",function (obj) {
    	console.info(obj);
    	
    	if(obj.event === 'insert'){
    	  layer.open({
		      type: 2,
		      title: '插入停车场现金流',
		      maxmin: true,
		      shadeClose: true, //点击遮罩关闭层
		      area : ['560px' , '450px'],
		      content: 'Set.html',
		      btn: ['确定','取消'],
		      success: function (layero, index) {	  		    		  		    	
	  		    },
		      yes: function(index, layero){	
		    	  
		    	//   var win=$(layero).find("iframe")[0].contentWindow;
                  //  var formSubmit=layer.getChildFrame('#inp1', index);
						//获取子页面中的元素，给元素传值。两种方法都可行
                var inp = $(layero).find("iframe").contents();
                                       
		    	let cashFlowId=inp.find("#cashFlowId").val();
  		    	let charge=inp.find("#charge").val();
  		    	let startTime=inp.find("#startTime").val();
  		    	let datas={'cashFlowId':cashFlowId,'charge':charge,'startTime':startTime};
  		    	 console.info(cashFlowId);
  		    	console.info(charge);
  		    	console.info(startTime);
				  $.ajax({
	  			        type: "post",
	  			        url: "/Manage/insertCash",
	  			        data:datas,
	  			        success: function (obj) { 	
	  			        	
	  			        	window.location.reload();
	  			        },
				        error:function(){
				        	layer.alert("添加数据失败")
				        }
	  			    });
				  
		      }
		  });
    	};
    });
    
   //行工具
    table.on("tool(indexTable)",function (obj) {
    	 
        let data = obj.data;
 
        if(obj.event === 'update'){
      	  layer.open({
  		      type: 2,
  		      title: '修改停车场现金流',
  		      maxmin: true,
  		      shadeClose: true, //点击遮罩关闭层
  		      area : ['560px' , '450px'],
  		      content: 'Set.html',
  		    btn: ['确定','取消'],
  		    success: function (layero, index) {
  		      var inp = $(layero).find("iframe").contents();
              //document.getElementById("EleId").style.display="none";
		    	let cashFlowId=inp.find("#hid").hide();
		    	let charge=inp.find("#charge").val(obj.data.charge);
		    	let startTime=inp.find("#startTime").val(obj.data.startTime);
		    	 		    	
  		    },
  		      yes: function(index, layero){	  
  		    	  var inp = $(layero).find("iframe").contents();
                  
  		    	    let cashFlowId=obj.data.cashFlowId;
    		    	let charge=inp.find("#charge").val();
    		    	let startTime=inp.find("#startTime").val();
    		    	let datas={'cashFlowId':cashFlowId,'charge':charge,'startTime':startTime};
  			   $.ajax({
  			        type: "post",
  			        url: "/Manage/updateCash",
  			        data:datas,
  			        success: function (obj) {
  			        	window.location.reload();
  			        }
  			    });
  			 
  		      }
  		  });
      	}else if(obj.event == 'delect'){
      	   $.ajax({
      	        type: "post",
      	        url: "/Manage/delectCash",
      	        data:{'cashFlowId':obj.data.cashFlowId},
      	        success: function (obj) {
      	        	window.location.reload();
      	        }
      	    });
      	}

      });
    
//    table.on("sort(indexTable)", function (obj) {
//        if (obj.type === "desc"){ //降序
//            let newData = JSON.parse(JSON.stringify(data)); //为了不改变原始排序，排序时拷贝对象
//            newData.sort(function (data1,data2) { //data1和data2是冒泡排序的左右值
//                return data2.startTime-data1.startTime;
//            });
//            indexTable.reload({
//                data: newData
//            })
//        }else if (obj.type === "asc"){ //升序
//            let newData = JSON.parse(JSON.stringify(data)); //为了不改变原始排序，排序时拷贝对象
//            newData.sort(function (data1,data2) { //data1和data2是冒泡排序的左右值
//                return data1.startTime-data2.startTime;
//            });
//            indexTable.reload({
//                data: newData
//            })
//        }else if (obj.type === null) { //原始顺序
//            let newData = JSON.parse(JSON.stringify(data));
//            indexTable.reload({
//                data: newData
//            });
//        }
//    });
});